package ar.edu.itba.it.cg.grupo01.game.terrain;

import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.shape.Box;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

public class DaytonaCP extends Checkpoint {

	private static final long serialVersionUID = 1L;
	private Node daytonaNode;
	
	public DaytonaCP(String name, Vector3f pos, float move, URL imageURL) {
		super(name, pos);
		buildDaytonaCP(name, move, imageURL, pos);
	}

	private void buildDaytonaCP(String name, float rotation, URL imageURL,
			Vector3f pos) {
		// Create the actual forcefield
		// The first box handles the X-axis, the second handles the z-axis.
		// We don't rotate the box as a demonstration on how boxes can be
		// created differently.
		Box daytonaBox = new Box(name + ".checkPoint", new Vector3f(0, 0, 0),
				70, 45, 0.1f);

		daytonaBox.setModelBound(new BoundingBox());
		daytonaBox.updateModelBound();

		// We are going to share these boxes as well
		SharedMesh forceFieldX1 = new SharedMesh(name + ".forceFieldX1",
				daytonaBox);

		// add all the force fields to a single node and make this node part of
		// the transparent queue.
		daytonaNode = new Node(name);

		daytonaNode.attachChild(forceFieldX1);

		float[] angles = new float[] { 0f, rotation, 0f };
		daytonaNode.setLocalRotation(new Quaternion(angles));
		//forceFieldNode.setLocalTranslation(pos.x, pos.y, pos.z);

		// load a texture for the force field elements
		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer()
				.createTextureState();
		Texture t = TextureManager.loadTexture(imageURL,
				MinificationFilter.Trilinear, MagnificationFilter.Bilinear);
		ts.setTexture(t);

		BlendState blendState = DisplaySystem.getDisplaySystem().getRenderer()
				.createBlendState();
		blendState.setBlendEnabled(true);
		blendState.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		blendState
				.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		blendState.setTestEnabled(true);
		blendState
				.setTestFunction(BlendState.TestFunction.GreaterThan);
		blendState.setEnabled(true);

		
		daytonaNode.setRenderState(ts);
		daytonaNode.setRenderState(blendState);
		
		daytonaNode.updateRenderState();

		// Attach all the pieces to the main dayt node
		this.attachChild(daytonaNode);

	}
	
}
